//
// Created by 32827 on 2023/11/17.
//
#include "yzf_uart.h"
YZF_RESULT yzf_uart_source_clock_open(YZF_UART_POINTER uart){
    switch ((uint32_t) uart) {
        case (uint32_t)USART0:
            rcu_periph_clock_enable(RCU_USART0);
            break;
        case (uint32_t)USART1:
            rcu_periph_clock_enable(RCU_USART1);
            break;
        case (uint32_t)USART2:
            rcu_periph_clock_enable(RCU_USART2);
            break;
        case (uint32_t)UART3:
            rcu_periph_clock_enable(RCU_UART3);
            break;
        case (uint32_t)UART4:
            rcu_periph_clock_enable(RCU_UART4);
            break;
        case (uint32_t)USART5:
            rcu_periph_clock_enable(RCU_USART5);
            break;
        case (uint32_t)UART6:
            rcu_periph_clock_enable(RCU_UART6);
            break;
        case (uint32_t)UART7:
            rcu_periph_clock_enable(RCU_UART7);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
//串口使能
YZF_RESULT yzf_uart_enable(YZF_UART_POINTER uart){
    usart_enable(uart);
    return YZF_OK;
}
//发送数据寄存器空
YZF_BOOL yzf_uart_flag_TXE(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_TBE) ? YZF_TRUE : YZF_FALSE;
}
//接收数据寄存器非空
YZF_BOOL yzf_uart_flag_RXNE(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_RBNE)? YZF_TRUE : YZF_FALSE;
}
//发送完成
YZF_BOOL yzf_uart_flag_TC(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_TC) ? YZF_TRUE : YZF_FALSE;
}
//监测到总线空闲 一次性发送了8个字节，就会产生8次RXNE中断，1次IDLE中断
YZF_BOOL yzf_uart_flag_IDLE(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_IDLE) ? YZF_TRUE : YZF_FALSE;
}
//过载位 当前数据未被读取，再次接收到数据时
YZF_BOOL  yzf_uart_flag_ORE(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_ORERR) ? YZF_TRUE : YZF_FALSE;
}
//噪声错误
YZF_BOOL yzf_uart_flag_NE(YZF_UART_POINTER uart){
    return usart_flag_get(uart,USART_FLAG_NERR) ? YZF_TRUE : YZF_FALSE;
}
//帧错误
YZF_BOOL yzf_uart_flag_FE(YZF_UART_POINTER uart){
    return  usart_flag_get(uart,USART_FLAG_FERR) ? YZF_TRUE : YZF_FALSE;
}
//校验错位
YZF_BOOL yzf_uart_flag_PE(YZF_UART_POINTER uart){
    return  usart_flag_get(uart,USART_FLAG_PERR) ? YZF_TRUE : YZF_FALSE;
}
//获取数据寄存器值
YZF_UART_DATA yzf_uart_get_data(YZF_UART_POINTER uart){
    return usart_data_receive(uart);
}
//设置数据寄存器值
YZF_RESULT yzf_uart_set_data(YZF_UART_POINTER uart, YZF_UART_DATA data){
    usart_data_transmit(uart,data);
    return YZF_OK;
}
//设置波特率
YZF_RESULT yzf_uart_baud_rate(YZF_UART_POINTER uart, uint32_t data){
    usart_baudrate_set(uart,data);
    return YZF_OK;
}
//设置字长
YZF_RESULT yzf_uart_word_length(YZF_UART_POINTER uart, YZF_UART_WORD_LENGTH length){
    usart_word_length_set(uart,length);
    return YZF_OK;
}
//当总线空闲时唤醒
YZF_RESULT yzf_uart_set_wakeup_Idle(YZF_UART_POINTER uart){
    usart_mute_mode_wakeup_config(uart,USART_WM_IDLE);
    return YZF_OK;
}
//当地址标记唤醒
YZF_RESULT yzf_uart_set_wakeup_Address(YZF_UART_POINTER uart){
    usart_mute_mode_wakeup_config(uart,USART_WM_ADDR);
    return YZF_OK;
}
//开启/关闭校验
YZF_RESULT yzf_uart_set_parity(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_parity_config(uart,enable ? USART_PM_ODD : USART_PM_NONE);
    return YZF_OK;
}
//奇校验
YZF_RESULT yzf_uart_set_parity_odd(YZF_UART_POINTER uart){
    usart_parity_config(uart, USART_PM_ODD);
    return YZF_OK;
}
//偶校验
YZF_RESULT yzf_uart_set_parity_even(YZF_UART_POINTER uart){
    usart_parity_config(uart, USART_PM_EVEN);
    return YZF_OK;
}
//PE校验中断使能
YZF_RESULT yzf_uart_pe_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_PERR);
    }else{
        usart_interrupt_disable(uart,USART_INT_PERR);
    }
    return YZF_OK;
}
//发送缓冲区空中断使能
YZF_RESULT yzf_uart_txe_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_TBE);
    }else{
        usart_interrupt_disable(uart,USART_INT_TBE);
    }
    return YZF_OK;
}
//接收缓冲区空中断使能
YZF_RESULT yzf_uart_rxne_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_RBNE);
    }else{
        usart_interrupt_disable(uart,USART_INT_RBNE);
    }
    return YZF_OK;
}
//发送完成中断使能
YZF_RESULT yzf_uart_tc_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_TC);
    }else{
        usart_interrupt_disable(uart,USART_INT_TC);
    }
    return YZF_OK;
}
//IDLE中断使能,一帧发送完毕
YZF_RESULT yzf_uart_idle_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_IDLE);
    }else{
        usart_interrupt_disable(uart,USART_INT_IDLE);
    }
    return YZF_OK;
}
//TE 发送使能
YZF_RESULT yzf_uart_te_enable(YZF_UART_POINTER uart){
    usart_transmit_config(uart,USART_TRANSMIT_ENABLE);
    return YZF_OK;
}
YZF_RESULT yzf_uart_te_disable(YZF_UART_POINTER uart){
    usart_transmit_config(uart,USART_TRANSMIT_DISABLE);
    return YZF_OK;
}
//RE 接收使能 当为1时位静默模式
YZF_RESULT yzf_uart_re_enable(YZF_UART_POINTER uart){
    usart_receive_config(uart,USART_RECEIVE_ENABLE);
    return YZF_OK;
}
YZF_RESULT yzf_uart_re_disable(YZF_UART_POINTER uart){
    usart_receive_config(uart,USART_RECEIVE_DISABLE);
    return YZF_OK;
}
//接受唤醒
YZF_RESULT yzf_uart_re_wakeup(YZF_UART_POINTER uart){
//    usart_mute_mode_wakeup_config(uart,USART_WM);
    return YZF_ERROR;
}
//发送断开帧
YZF_RESULT yzf_uart_send_disconnect_frame(YZF_UART_POINTER uart){
    usart_stop_bit_set(uart,USART_STB_1BIT);
    return YZF_OK;
}
//LIN模式使能
YZF_RESULT yzf_uart_lin(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_lin_mode_enable(uart);
    return YZF_OK;
}
//停止位 0-1 1-0.5 2-2 3-1.5
YZF_RESULT yzf_uart_stop_bit(YZF_UART_POINTER uart,YZF_UART_DATA stopbits){
    uint32_t stblen=0;
    switch (stopbits) {
        case 0:
            stblen=USART_STB_1BIT;
            break;
        case 1:
            stblen=USART_STB_0_5BIT;
            break;
        case 2:
            stblen=USART_STB_2BIT;
            break;
        case 3:
            stblen=USART_STB_1_5BIT;
            break;
        default:
            return YZF_ERROR;
    }
    usart_stop_bit_set(uart,stblen);
    return YZF_OK;
}
//时钟使能
YZF_RESULT yzf_uart_clock(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_synchronous_clock_enable(uart);
    }else{
        usart_synchronous_clock_disable(uart);
    }
    return YZF_OK;
}
uint32_t ph=USART_CPH_1CK;
uint32_t po=USART_CPL_HIGH;
//设置时钟极性 isHigh空闲时是否保持高电位
YZF_RESULT yzf_uart_clock_polarity(YZF_UART_POINTER uart,YZF_BOOL isHigh){
    if(isHigh){
        usart_synchronous_clock_config(uart,USART_CLEN_NONE,ph,USART_CPL_HIGH);
        po=USART_CPL_HIGH;
    }else{
        usart_synchronous_clock_config(uart,USART_CLEN_NONE,ph,USART_CPL_LOW);
        po=USART_CPL_LOW;
    }
    return YZF_OK;
}
//设置时钟相位 isSecond是否在第二个边沿捕获
YZF_RESULT yzf_uart_clock_phase(YZF_UART_POINTER uart,YZF_BOOL isFirst){
    if(isFirst){
        usart_synchronous_clock_config(uart,USART_CLEN_NONE,USART_CPH_1CK,po);
        ph=USART_CPH_1CK;
    }else{
        usart_synchronous_clock_config(uart,USART_CLEN_NONE,USART_CPH_2CK,po);
        ph=USART_CPH_2CK;
    }
    return YZF_OK;
}
//LIN断开符中断使能
YZF_RESULT yzf_uart_lin_disconnect_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_LBD);
    }else{
        usart_interrupt_disable(uart,USART_INT_LBD);
    }
    return YZF_OK;
}
//LIN断开符检测长度 true 11位检测 false 10位检测
YZF_RESULT yzf_uart_lin_disconnect_length(YZF_UART_POINTER uart,YZF_BOOL is11bits){
    if(is11bits){
        usart_lin_break_detection_length_config(uart,USART_LBLEN_11B);
    }else{
        usart_lin_break_detection_length_config(uart,USART_LBLEN_10B);
    }
    return YZF_OK;
}
//设备节点地址 0b0000 4位
YZF_RESULT yzf_uart_node_address(YZF_UART_POINTER uart,YZF_UART_DATA address){
    usart_address_config(uart,address);
    return YZF_OK;
}
//CTS中断使能
YZF_RESULT yzf_uart_cts_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_CTS);
    }else{
        usart_interrupt_disable(uart,USART_INT_CTS);
    }
    return YZF_OK;
}
//CTS使能
YZF_RESULT yzf_uart_cts_enable(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_hardware_flow_cts_config(uart,enable);
    return YZF_OK;
}
//RTS中断使能 当有空闲时才请求下一个数据
YZF_RESULT yzf_uart_rts_enable(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_hardware_flow_rts_config(uart,enable);
    return YZF_OK;
}
//DMA发送使能
YZF_RESULT yzf_uart_dma_transmitter_enable(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_dma_transmit_config(uart,enable);
    return YZF_OK;
}
//DMA接收使能
YZF_RESULT yzf_uart_dma_receiver_enable(YZF_UART_POINTER uart,YZF_BOOL enable){
    usart_dma_receive_config(uart,enable);
    return YZF_OK;
}
//半双工模式
YZF_RESULT yzf_uart_half_duplex(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_halfduplex_enable(uart);
    }else{
        usart_halfduplex_disable(uart);
    }
    return YZF_OK;
}
//错误中断使能
YZF_RESULT yzf_uart_error_interrupt(YZF_UART_POINTER uart,YZF_BOOL enable){
    if(enable){
        usart_interrupt_enable(uart,USART_INT_ERR);
    }else{
        usart_interrupt_disable(uart,USART_INT_ERR);
    }
    return YZF_OK;
}